package com.mapzone.showmap;

import com.mapzone.showmap.R.color;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.FloatMath;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.MotionEvent;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MapActivityNew extends Activity {

	TextView doc_info;
	TextView curr_info;
	Button bt_edit;
	Button bt_general;
	Button bt_menu;
	
	Button bt_collect_point;
	Button bt_collect_line;
	Button bt_collect_plane;
	Button bt_pan;
	Button bt_region;
	Button bt_select;
	Button bt_entire;
	Button bt_info;
	Button bt_sql;
	Button bt_zoom_out;
	Button bt_zoom_in;
	
	ImageButton ibt_point;
	ImageButton ibt_line;
	ImageButton ibt_continuous_point;
	ImageButton ibt_gnss_point;
	ImageButton ibt_gnss_seting;
	ImageButton ibt_xy_point;
	ImageButton ibt_reverse;
	ImageButton ibt_trace_line;
	ImageButton ibt_collect_ok;
	ImageButton ibt_collect_cancel;
	
	LinearLayout ly_edit;
	LinearLayout ly_general;
	LinearLayout ly_edit_sub;
	
	int bg_color;
	String str_curr_info;
	String str_curr_info_sub;
	String str_curr_info_sub_sub;
	
//	MapViewNew map_view;
	MapView map_view;
	String map_doc_path;
	private LocationManager location_manager;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 
		requestWindowFeature(Window.FEATURE_NO_TITLE);  
		
		super.onCreate(savedInstanceState);
		setContentView( R.layout.map_activity_new );
		
		view_init();
		
		Intent intent = getIntent();
		map_doc_path = intent.getStringExtra( "path" );
		
//		map_view.screen = BitmapFactory.decodeStream( getResources().openRawResource( R.drawable.screen ) );
		DisplayMetrics dm = new DisplayMetrics();
		this.getWindowManager().getDefaultDisplay().getMetrics(dm);
		map_view.screen = Bitmap.createBitmap( dm.heightPixels, dm.widthPixels, Config.ARGB_8888 );
		
		location_manager = ( LocationManager )this.getSystemService( Context.LOCATION_SERVICE );

		map_view.init( dm.xdpi,  dm.ydpi );
		map_view.init_bitmap( dm.widthPixels, dm.heightPixels );
		map_view.init_ndk( dm.widthPixels, dm.heightPixels, dm.xdpi, dm.ydpi );
		map_view.open_map_doc( map_doc_path );
		map_view.Invalidate( 0 );
	}
	
	@Override
	protected void onRestart() {
		super.onRestart();
		map_view.Invalidate( 0 );
	}

	@Override
	protected void onResume() {
		super.onResume();
		map_view.Invalidate( 0 );
	}

	@Override
	protected void onStart() {
		super.onStart();
		map_view.Invalidate( 0 );
	}
	
	private boolean b_double = false;
	private float old_spacing = 0;
	private boolean b_one_down = false;
	private float scale = 0;
	private float x_old = 0;
	private float y_old = 0;
	public boolean onTouchEvent( MotionEvent event ) 
	{
		float x = event.getX();
		float y = event.getY();
		switch( event.getAction() )
		{
		case MotionEvent.ACTION_DOWN:
			Log.v( "SHOWMAP", "ACTION_DOWN"  );
			this.x_old = x;
			this.y_old = y;
			b_one_down = true;
			map_view.on_touch_down( x, y );
			break;
		case MotionEvent.ACTION_MOVE:
			Log.v( "SHOWMAP", "ACTION_MOVE"  );
			//			mv.layout( ( int )( x - this.x ), ( int )( y - this.y ), ( int )( x + mv.getWidth() - this.x ), ( int )( y + mv.getHeight() - this.y ) );
//			if( b_double )
//			{
//				float new_spacing = calculate_spacing( event );
//				scale = new_spacing - old_spacing;
//			}
//			else
//			{
				if( !( Math.abs( (int)( x- x_old ) ) < 5 && Math.abs( (int)( y - y_old ) ) < 5 ) )
				{
					map_view.on_touch_move( x, y );
				}
//			}
			break;
		case MotionEvent.ACTION_UP:
			Log.v( "SHOWMAP", "ACTION_UP"  );
			//如果双指同时离开不会触发action_pointer_2_up，会直接触发action_up
//			if( b_double )
//			{
//				Log.v( "SHOWMAP", "ACTION_POINTER_2_UP scale = " + scale  );
//				int i_scale = ( ( int )scale / 60 ) + 1;
//				map_view.scale_zoom( i_scale );
//				b_double = false;
//			}
//			else
//			{
				b_one_down = false;
				map_view.on_touch_up( x, y );
//			}
			//			mv.pan( x - this.x, y - this.y );
			break;
		case MotionEvent.ACTION_POINTER_2_DOWN:
					Log.v( "SHOWMAP", "ACTION_POINTER_2_DOWN"  );
//					old_spacing = calculate_spacing( event );
//					//双指点击时会先触发的是单指落下，这里把这一次事件完成。
//					b_one_down = false;
//					b_double = true;
//					map_view.on_touch_up( x_old, y_old );
			break;
		case MotionEvent.ACTION_POINTER_2_UP:
//					Log.v( "SHOWMAP", "ACTION_POINTER_2_UP" );
//					float new_spacing = calculate_spacing( event );
//					float scale = new_spacing - old_spacing;
//					Log.v( "SHOWMAP", "ACTION_POINTER_2_UP scale = " + scale  );
//					mv.scale_zoom( scale );
			break;
		}
		return super.onTouchEvent(event);
	}
	
	private float calculate_spacing(MotionEvent event) {
		float x = event.getX(0) - event.getX(1);
		float y = event.getY(0) - event.getY(1);
		return FloatMath.sqrt(x * x + y * y);
	}

	public class MyOnCLickListener implements OnClickListener
	{
		public MyOnCLickListener()
		{
			super();
		}
		
		public void onClick( View v )
		{
			switch( v.getId() )
			{
			case R.id.bt_edit:
			case R.id.bt_general:
				head_button_group( v );
				break;
			case R.id.bt_collect_point:
			case R.id.bt_collect_line:
			case R.id.bt_collect_plane:
				collect_layout_group( v );
				break;
			case R.id.bt_pan:
			case R.id.bt_region:
			case R.id.bt_select:
			case R.id.bt_entire:
			case R.id.bt_info:
			case R.id.bt_sql:
			case R.id.bt_zoom_in:
			case R.id.bt_zoom_out:
				general_layout_group( v );
				break;
			case R.id.ibt_point:
			case R.id.ibt_line:
			case R.id.ibt_continuous_point:
			case R.id.ibt_gnss_point:
			case R.id.ibt_gnss_seting:
			case R.id.ibt_xy_point:
			case R.id.ibt_reverse:
			case R.id.ibt_trace_line:
			case R.id.ibt_collect_ok:
			case R.id.ibt_collect_cancel:
				collect_subtool_layout_group( v );
				break;
			}
			map_view.Invalidate( 0 );
		}
		
		private void head_button_group( View v )
		{
			switch( v.getId() )
			{
			case R.id.bt_edit:
				if( ly_general.getVisibility() == View.VISIBLE )
				{
					ly_general.setVisibility( View.GONE );
					bt_general.setBackgroundDrawable( getResources().getDrawable( R.drawable.ibt_general ) );
				}
				if( ly_edit.getVisibility() == View.VISIBLE )
				{
					ly_edit.setVisibility( View.GONE );
					ly_edit_sub.setVisibility( View.GONE );
					bt_edit.setBackgroundDrawable( getResources().getDrawable( R.drawable.ibt_edit));
				}
				else
				{
					ly_edit.setVisibility( View.VISIBLE );
					bt_edit.setBackgroundDrawable( getResources().getDrawable( R.drawable.ibt_edit_pressed));
					str_curr_info = "采编工具";
				}
				break;
			case R.id.bt_general:
				ly_edit_sub.setVisibility( View.GONE );
				if( ly_edit.getVisibility() == View.VISIBLE )
				{
					ly_edit.setVisibility( View.GONE );
					ly_edit_sub.setVisibility( View.GONE );
					bt_edit.setBackgroundDrawable( getResources().getDrawable( R.drawable.ibt_edit));
				}
				if( ly_general.getVisibility() == View.VISIBLE )
				{
					ly_general.setVisibility( View.GONE );
					bt_general.setBackgroundDrawable( getResources().getDrawable( R.drawable.ibt_general ) );
				}
				else
				{
					ly_general.setVisibility( View.VISIBLE );
					bt_general.setBackgroundDrawable( getResources().getDrawable( R.drawable.ibt_general_pressed ) );
					str_curr_info = "通用工具";
				}
				break;
			case R.id.bt_menu:
				break;
			}
			curr_info.setText( str_curr_info );
		}
		
		private void collect_layout_group( View v )
		{
			ly_edit_sub.setVisibility( View.VISIBLE );
			switch( v.getId() )
			{
			case R.id.bt_collect_point:
				ibt_line.setEnabled( false );
				ibt_continuous_point.setEnabled( false );
				ibt_gnss_seting.setEnabled( false );
				ibt_reverse.setEnabled( false );
				ibt_trace_line.setEnabled( false );
				str_curr_info_sub = ">>>>>点采集";
				break;
			case R.id.bt_collect_line:
				ibt_line.setEnabled( true );
				ibt_continuous_point.setEnabled( true );
				ibt_gnss_seting.setEnabled( true );
				ibt_reverse.setEnabled( true );
				ibt_trace_line.setEnabled( true );
				str_curr_info_sub = ">>>>>线采集";
				break;
			case R.id.bt_collect_plane:
				ibt_line.setEnabled( true );
				ibt_continuous_point.setEnabled( true );
				ibt_gnss_seting.setEnabled( true );
				ibt_reverse.setEnabled( true );
				ibt_trace_line.setEnabled( true );
				str_curr_info_sub = ">>>>>面采集";
				break;
			}
			curr_info.setText( str_curr_info + str_curr_info_sub );
		}
		
		private void collect_subtool_layout_group( View v )
		{
			switch ( v.getId() )
			{
			case R.id.ibt_point:
				str_curr_info_sub_sub = ">>>>>屏幕采点";
				break;
			case R.id.ibt_line:
				str_curr_info_sub_sub = ">>>>>手绘线";
				break;
			case R.id.ibt_continuous_point:
				str_curr_info_sub_sub = ">>>>>gnss连续采点";
				break;
			case R.id.ibt_gnss_point:
				str_curr_info_sub_sub = ">>>>>gnss平均值采点";
				break;
			case R.id.ibt_gnss_seting:
				str_curr_info_sub_sub = ">>>>>gnss设置";
				break;
			case R.id.ibt_xy_point:
				str_curr_info_sub_sub = ">>>>>输入xy值";
				break;
			case R.id.ibt_reverse:
				str_curr_info_sub_sub = ">>>>>反向采集";
				break;
			case R.id.ibt_trace_line:
				str_curr_info_sub_sub = ">>>>>线追踪";
				break;
			case R.id.ibt_collect_ok:
				str_curr_info_sub_sub = ">>>>>完成采集";
				break;
			case R.id.ibt_collect_cancel:
				str_curr_info_sub_sub = ">>>>>取消采集";
				break;
			}
			curr_info.setText( str_curr_info + str_curr_info_sub + str_curr_info_sub_sub );
		}
		
		private void general_layout_group( View v )
		{
			switch( v.getId() )
			{
			case R.id.bt_pan:
				str_curr_info_sub = ">>>>>平移工具";
				map_view.pan();
				break;
			case R.id.bt_region:
				str_curr_info_sub = ">>>>>拉框缩放工具";
				map_view.region_zoom();
				break;
			case R.id.bt_select:
				str_curr_info_sub = ">>>>>选择工具";
				map_view.select();
				break;
			case R.id.bt_entire:
				str_curr_info_sub = ">>>>>全图";
				map_view.entire();
				break;
			case R.id.bt_info:
				str_curr_info_sub = ">>>>>I 键查询";
				break;
			case R.id.bt_sql:
				str_curr_info_sub = ">>>>>sql查询";
				break;
			case R.id.bt_zoom_in:
				str_curr_info_sub = ">>>>>放大";
				break;
			case R.id.bt_zoom_out:
				str_curr_info_sub = ">>>>>缩小";
				break;
			}
			curr_info.setText( str_curr_info + str_curr_info_sub );
		}
	}
	
	public void view_init()
	{
		map_view = ( MapView )findViewById( R.id.map_view );

		bt_edit = ( Button )findViewById( R.id.bt_edit );
		bt_edit.setOnClickListener( new MyOnCLickListener() );
		bt_edit.setBackgroundDrawable( getResources().getDrawable( R.drawable.ibt_edit));
		bt_edit.setTextColor( Color.parseColor( "#E22F2F" ) );
		bt_general = ( Button )findViewById( R.id.bt_general );
		bt_general.setOnClickListener( new MyOnCLickListener() );
		bt_general.setBackgroundDrawable( getResources().getDrawable( R.drawable.ibt_general ) );
		bt_general.setTextColor( Color.parseColor( "#E22F2F" ) );
		bt_menu = ( Button )findViewById( R.id.bt_menu );
		bt_menu.setOnClickListener( new MyOnCLickListener() );
		bt_collect_point = ( Button )findViewById( R.id.bt_collect_point );
		bt_collect_point.setOnClickListener( new MyOnCLickListener() );
		bt_collect_line = ( Button )findViewById( R.id.bt_collect_line );
		bt_collect_line.setOnClickListener( new MyOnCLickListener() );
		bt_collect_plane = ( Button )findViewById( R.id.bt_collect_plane );
		bt_collect_plane.setOnClickListener( new MyOnCLickListener() );
		bt_pan = ( Button )findViewById( R.id.bt_pan );
		bt_pan.setOnClickListener( new MyOnCLickListener() );
		bt_select = ( Button )findViewById( R.id.bt_select );
		bt_select.setOnClickListener( new MyOnCLickListener() );
		bt_entire = ( Button )findViewById( R.id.bt_entire );
		bt_entire.setOnClickListener( new MyOnCLickListener() );
		bt_region = ( Button )findViewById( R.id.bt_region );
		bt_region.setOnClickListener( new MyOnCLickListener() );
		bt_info = ( Button )findViewById( R.id.bt_info );
		bt_info.setOnClickListener( new MyOnCLickListener() );
		bt_sql = ( Button )findViewById( R.id.bt_sql );
		bt_sql.setOnClickListener( new MyOnCLickListener() );
		bt_zoom_in = ( Button )findViewById( R.id.bt_zoom_in );
		bt_zoom_in.setOnClickListener( new MyOnCLickListener() );
		bt_zoom_out = ( Button )findViewById( R.id.bt_zoom_out );
		bt_zoom_out.setOnClickListener( new MyOnCLickListener() );
		ibt_point = ( ImageButton )findViewById( R.id.ibt_point );
		ibt_point.setOnClickListener( new MyOnCLickListener() );
		ibt_line = ( ImageButton )findViewById( R.id.ibt_line );
		ibt_line.setOnClickListener( new MyOnCLickListener() );
		ibt_continuous_point = ( ImageButton )findViewById( R.id.ibt_continuous_point );
		ibt_continuous_point.setOnClickListener( new MyOnCLickListener() );
		ibt_gnss_point = ( ImageButton )findViewById( R.id.ibt_gnss_point );
		ibt_gnss_point.setOnClickListener( new MyOnCLickListener() );
		ibt_gnss_seting = ( ImageButton )findViewById( R.id.ibt_gnss_seting );
		ibt_gnss_seting.setOnClickListener( new MyOnCLickListener() );
		ibt_xy_point = ( ImageButton )findViewById( R.id.ibt_xy_point );
		ibt_xy_point.setOnClickListener( new MyOnCLickListener() );
		ibt_reverse = ( ImageButton )findViewById( R.id.ibt_reverse );
		ibt_reverse.setOnClickListener( new MyOnCLickListener() );
		ibt_trace_line = ( ImageButton )findViewById( R.id.ibt_trace_line );
		ibt_trace_line.setOnClickListener( new MyOnCLickListener() );
		ibt_collect_ok = ( ImageButton )findViewById( R.id.ibt_collect_ok );
		ibt_collect_ok.setOnClickListener( new MyOnCLickListener() );
		ibt_collect_cancel = ( ImageButton )findViewById( R.id.ibt_collect_cancel );
		ibt_collect_cancel.setOnClickListener( new MyOnCLickListener() );

		//		da_default_background = bt_collect_line.getBackground();
		bg_color = bt_collect_line.getSolidColor();

		doc_info = ( TextView )findViewById( R.id.text_doc_info );
		doc_info.setBackgroundColor( Color.parseColor( "#C8DEAE" ) );
		doc_info.getBackground().setAlpha( 100 );
		doc_info.setText( "地图文档" );

		curr_info = ( TextView )findViewById( R.id.text_curr_info );
		curr_info.setBackgroundColor( Color.parseColor( "#01FE01" ) );
		curr_info.getBackground().setAlpha( 80 );
		curr_info.setText( "浏览地图" );

		ly_edit = ( LinearLayout )findViewById( R.id.layout_edit_toolbar );
		ly_edit.setVisibility( View.GONE );
		ly_general = ( LinearLayout )findViewById( R.id.layout_general_toolbar );
		ly_general.setVisibility( View.GONE );
		ly_edit_sub = ( LinearLayout )findViewById( R.id.layout_edit_sub );
		ly_edit_sub.setVisibility( View.GONE );
	}
}
